<?php
class Detail extends MY_Controller
{
	public function __construct()
	{
		log_message('debug', '开始调用Item->Detail->__construct接口');
		parent::__construct();
		$this->loadLanguageFile('application/controllers/Item/');
		log_message('debug', '调用Item->Detail->__construct接口结束');
	}
	
	public function showView() {
		$this->load->view('module/Item/detail.htm');
	}
	
	public function getListData() {
	    log_message('debug', '=====开始调用Item->Detail->getListData接口=====');
		$this->verifyPrivilege();
		
		try {
			$gameAreaId = $this->param->getParam('gameAreaId', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 201, 'invalidErrorCode' => 1));
			$account = $this->param->getParam('account', array('method' => 'string', 'emptyErrorCode' => 1, 'invalidErrorCode' => 1), 'post', true);
			$roleName = $this->param->getParam('roleName', array('method' => 'string', 'emptyErrorCode' => 1, 'invalidErrorCode' => 1), 'post', true);
			$startDateTime = $this->param->getParam('startDateTime', array('method' => 'string', 'emptyErrorCode' => 1, 'invalidErrorCode' => 1), 'post', true);
			$endDateTime = $this->param->getParam('endDateTime', array('method' => 'string', 'emptyErrorCode' => 1, 'invalidErrorCode' => 1), 'post', true);
			$id = $this->param->getParam('id', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 1, 'invalidErrorCode' => 1), 'post', true);
			$page = $this->param->getParam('page', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 1, 'invalidErrorCode' => 1), 'post', true);
			$rows = $this->param->getParam('rows', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 1, 'invalidErrorCode' => 1), 'post', true);
				
			if( !$account && !$roleName ) {
				throw new Exception('', 202);
			}
			
			$this->initGameAreaInfo($gameAreaId);
			
			$sql = "
				select
					`userId`
				from
					`t_data_game_account`
				where
					(
						`account` = '{$account}'
						or
						`roleName` = '{$roleName}'
					)
					and
					`gameAreaId` = '{$gameAreaId}'
			";
				
			if( ($row = $this->gmDatabase->getRow($sql)) === false) {
				throw new Exception('', 204);
			}
				
			$userId = $row['userId'];
			
			if($id) {
				$sql = "
					select
						`itemClass`,
						`itemType`,
						`itemId`
					from
						`t_cfg_item`
					where
						`id` = '{$id}'
				";
				
				if( ( $row = $this->gmDatabase->getRow($sql) ) === false ) {
					throw new Exception('', 205);
				}
				else {
					$itemClass = $row['itemClass'];
					$itemType = $row['itemType'];
					$itemId = $row['itemId'];
				}
			}
			
			$sql = "
				select
					<selectColumns>
				from
					`t_data_game_item`
				where
					`userId` = '{$userId}'
			";
				
			if($startDateTime) {
				$sql .= " and `operatorDateTime` >= '{$startDateTime}'";
			}
			
			if($endDateTime) {
				$sql .= " and `operatorDateTime` <= '{$endDateTime}'";
			}
			
			if($id) {
				$sql .= " and `itemClass` = '{$itemClass}'";
				$sql .= " and `itemType` = '{$itemType}'";
				$sql .= " and `itemId` = '{$itemId}'";
			}
			
			$getTotalSql = str_replace('<selectColumns>', "count(*)", $sql);
			
			$this->returnData['data']['total'] = $this->gmDatabase->getColumn($getTotalSql);
			$getDataSql = str_replace('<selectColumns>', "`functionId`, `subFunctionId`, `itemClass`, `itemType`, `itemId`, `changeValue`, `operatorDateTime`", $sql);
			
			if($page && $rows) {
				$startRow = ($page - 1) * $rows;
				$getDataSql .= " limit {$startRow}, {$rows}";
			}
			
			$tmpArray = $this->gmDatabase->getArray($getDataSql);
			
			$sql = "
				select
					`itemClass`,
					`itemType`,
					`itemId`,
					`item{$this->currLanguage}Name` as `itemName`
				from
					`t_cfg_item`
			";
			
			$itemArray = $this->gmDatabase->getArray($sql);
			
			foreach($itemArray as $key => $val) {
				$itemInfo["{$val['itemClass']}-{$val['itemType']}-{$val['itemId']}"] = $val['itemName'];
			}
			
			$sql = "
				select
					a.`functionId`,
					if(b.`subFunctionId` is null, 0, b.`subFunctionId`) as subFunctionId,
					a.`function{$this->currLanguage}Name` as functionName,
					b.`subFunction{$this->currLanguage}Name` as subFunctionName
				from
					`t_cfg_item_function` a
				left join
					`t_cfg_item_subfunction` b
				on
					a.`functionId` = b.`functionId`
			";
			
			$functionArray = $this->gmDatabase->getArray($sql);
				
			foreach ($functionArray as $key => $val) {
				$functionInfo["{$val['functionId']}-{$val['subFunctionId']}"] = array(
					"functionName"	=>	$val['functionName'],
					"subFunctionName"	=>	$val['subFunctionName']
				);
			}
			
			foreach ($tmpArray as $key => $val) {
				if(isset($itemInfo["{$val['itemClass']}-{$val['itemType']}-{$val['itemId']}"])) {
					$tmpArray[$key]['itemName'] = $itemInfo["{$val['itemClass']}-{$val['itemType']}-{$val['itemId']}"];
				}
				else {
					$tmpArray[$key]['itemName'] = "{$val['itemClass']}-{$val['itemType']}-{$val['itemId']}";
				}
				
				if(isset($functionInfo["{$val['functionId']}-{$val['subFunctionId']}"])) {
					$tmpArray[$key]['functionName'] = $functionInfo["{$val['functionId']}-{$val['subFunctionId']}"]['functionName'];
					$tmpArray[$key]['subFunctionName'] = $functionInfo["{$val['functionId']}-{$val['subFunctionId']}"]['subFunctionName'];
				}
				else {
					$tmpArray[$key]['functionName'] = $tmpArray[$key]['functionId'];
					$tmpArray[$key]['subFunctionName'] = $tmpArray[$key]['subFunctionId'];
				}
			}
			
			$this->returnData['data']['rows'] = $tmpArray;
		}
		
		catch(MY_Param_Exception $e) {
			$this->setErrorCode($e->getCode());
		}
		
		catch(MY_Mysql_Exception $e) {
			$this->setErrorCode(1);
		}
		
		catch(Exception $e) {
			$this->setErrorCode($e->getCode());
		}
		
		$this->returnJson();
		log_message('debug', '=====调用Item->Detail->getListData接口结束=====');
	}
}